package com.mars.btrace.controller;

import com.alibaba.fastjson.JSON;
import com.mars.btrace.pojo.User;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * author by: mars
 * Date: 2019/5/26 14:34
 * Description:
 */

@RestController
@RequestMapping("/user")
public class UserController {

    /**
     *
     * @Author mars
     * @Description 拦截普通函数
     *
     * http://localhost:8080/user/getUserName?name=mars
     *
     */
    @RequestMapping("/getUserName")
    public String getUsername(@RequestParam("name") String name){
        return "username:" + name;
    }

    /**
     *
     * @Author mars
     * @Description 拦截构造函数
     *
     * http://localhost:8080/user/getConstruct?id=1&name=mars&sex=man
     *
     */
    @PostMapping(value = "/getConstruct")
    public String getConstruct(@RequestBody  @Valid User user, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return bindingResult.getFieldError().getDefaultMessage();
        }
        return JSON.toJSONString(user);
    }

    /**
     *
     * @Author mars
     * @Description 拦截同名方法
     *
     * http://localhost:8080/user/same1?name=mars
     */
    @RequestMapping("/same1")
    public String same(@RequestParam("name") String name){
        return "username:" + name;
    }

    /**
     *
     * @Author mars
     * @Description 拦截同名方法
     *
     * http://localhost:8080/user/same2?name=mars&hobby=tkd
     */
    @RequestMapping("/same2")
    public String same(@RequestParam("name") String name,
                       @RequestParam("hobby") String hobby){
        return "username:" + name + ",hobby:" + hobby;
    }


    /**
     *
     * @Author mars
     * @Description 拦截异常
     *
     * http://localhost:8080/user/exception
     */
    @RequestMapping("/exception")
    public String getException(){
        try{
            System.out.println("start...");
            System.out.println(1/0);
            System.out.println("end....");
        }catch (Exception e){

        }
        return "ok";
    }
}














